<script setup>
import { ref, nextTick } from 'vue'
import { Delete, Edit } from '@element-plus/icons-vue'
import ChannelSelect from './components/ChannelSelect.vue'
import ArticleEdit from './components/ArticleEdit.vue'
import { artGetListService } from '@/api/article.js'
import { formatTime } from '@/utils/format.js'
import { artDeleteService } from '../../api/article'
const articleList = ref([]) // 文章列表
const total = ref(0) // 总页数
const loading = ref(false)
// const visibleDrawer = ref(false) // 控制抽屉的显现与隐藏
const drawer = ref() // 抽屉组件

const params = ref({
  pagenum: 1,
  pagesize: 5,
  cate_id: '',
  state: ''
})
// 获取文章列表
const getArticleList = async () => {
  loading.value = true
  const res = await artGetListService(params.value)
  articleList.value = res.data.data
  total.value = res.data.total
  loading.value = false
}
getArticleList()

// 处理分页的逻辑
const handleSizeChange = (size) => {
  // 改变每页展示的数据量，就跳回第一页
  params.value.pagenum = 1
  params.value.pagesize = size
  getArticleList()
}
const handleCurrentChange = (value) => {
  params.value.pagenum = value
  getArticleList()
}

// 搜索文章逻辑
const onSearch = () => {
  params.value.pagenum = 1 // 页面回到1
  getArticleList()
}
// 重置搜索条件逻辑，将筛选条件清空
const onReset = () => {
  params.value.pagenum = 1 // 页面回到1
  params.value.cate_id = ''
  params.value.state = ''
  getArticleList()
}
// 添加文章逻辑
const onAddArticle = () => {
  drawer.value.open({})
}
// 编辑文章
const onEditArticle = (row) => {
  drawer.value.open(row)
}
// 添加或编辑文章成功的逻辑
const onSuccess = async (type) => {
  if (type === 'add') {
    // 如果是添加就跳到最后一页，也就是刚添加数据的位置
    const lastPage = Math.ceil((total.value + 1) / params.value.pagesize)
    // 更新成最大页码在渲染
    params.value.pagenum = lastPage
  }
  // 如果是编辑就直接渲染
  await nextTick()
  getArticleList()
}

// 删除文章
const onDeleteArticle = async (row) => {
  await ElMessageBox.confirm('你确定要删除这篇文章吗？', '温馨提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
  await artDeleteService(row.id)
  ElMessage.success('删除成功')
  getArticleList()
}
</script>
<template>
  <page-container title="文章管理">
    <template #extra>
      <el-button type="primary" @click="onAddArticle">添加文章</el-button>
    </template>
    <!-- 表单区域 -->
    <el-form inline>
      <el-form-item label="文章分类">
        <!-- vue2 => v-model是 :value 和 @input 的简写 -->
        <!-- vue3 => v-model是 :modelValue 和 @update:modelValue 的简写 -->
        <channel-select v-model="params.cate_id"></channel-select>
      </el-form-item>
      <el-form-item label="发布状态">
        <el-select v-model="params.state">
          <!-- 这里后台标记发布状态, 就是通过中文标记的 -->
          <el-option label="已发布" value="已发布"></el-option>
          <el-option label="草稿" value="草稿"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch">搜索</el-button>
        <el-button @click="onReset">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 表格区域 -->
    <el-table :data="articleList" v-loading="loading">
      <el-table-column label="文章标题" prop="title">
        <template #default="{ row }">
          <el-link type="primary" :underline="false">{{ row.title }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="分类" prop="cate_name"></el-table-column>
      <el-table-column label="发表时间" prop="pub_date">
        <template #default="{ row }">
          {{ formatTime(row.pub_date) }}
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="state"></el-table-column>
      <el-table-column label="操作">
        <template #default="{ row }">
          <el-button
            circle
            plain
            type="primary"
            :icon="Edit"
            @click="onEditArticle(row)"
          ></el-button>
          <el-button
            circle
            plain
            type="danger"
            :icon="Delete"
            @click="onDeleteArticle(row)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页区 -->
    <el-pagination
      v-model:current-page="params.pagenum"
      v-model:page-size="params.pagesize"
      :page-sizes="[2, 3, 5, 10]"
      :background="true"
      layout="jumper, total, sizes, prev, pager, next"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      style="margin-top: 20px; justify-content: flex-end"
    />

    <!-- 抽屉区 -->
    <article-edit ref="drawer" @success="onSuccess"></article-edit>
  </page-container>
</template>

<style lang="less" scoped></style>
